package com.powernode.ex.handler;

/**
 * @version 1.0
 * @Date 2024/8/15 10:22
 * @Author 付~~~
 */

import com.powernode.constant.BusinessEnum;
import com.powernode.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice       //增强全局异常处理类
@Slf4j  //日志处理
public class GlobalExceptionHandle {
    /**
     * 如果运行时报错runtimeException异常就走该路径
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeException(RuntimeException e){
        log.error(e.getMessage());
        return Result.fail(BusinessEnum.SERVER_INNER_FAIL);
    }

    /**
     * 拒绝访问异常时走该路径
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result<String> accessDeniedException(AccessDeniedException e){
        log.error(e.getMessage());
        throw e;
    }

    /**
     * 业务异常类
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public Result<String> businessException(BusinessException e){
        log.error(e.getMessage());
        return Result.fail(BusinessEnum.OPERATION_FAIL.getCode(),e.getMessage());
    }
}
